package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Service
@Transactional
public class SpuDetailServicempl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpecParamService paramService;

    @Override
    public List<SpecParamDTO> querySpecValues(Long id, Boolean searching) {
        //获取规格参数的键值对并转换为map集合
        SpuDetail detail = getById(id);
        Map<Long, Object> specValues = JsonUtils.toMap(detail.getSpecification(),Long.class,Object.class);
        //获取分类信息
        Spu spu = spuService.getById(id);
        List<SpecParam> params = paramService.queryParams(spu.getCid3(), null, searching);
        List<SpecParamDTO> specParamDTOS = SpecParamDTO.convertEntityList(params);
        for (SpecParamDTO paramDTO : specParamDTOS) {
            paramDTO.setValue(specValues.get(paramDTO.getId()));
        }
        return specParamDTOS;
    }
}
